<?php

/**
 * TaskConfigure
 *-------------------------
 *
 * Deal with menu display, selected status. Top menu - sub menu
 *
 * PHP versions 5
 *
 * LICENSE: This source file is from Smarter Ver4.0, which is a comprehensive shopping engine
 * that helps consumers to make smarter buying decisions online. We empower consumers to compare
 * the attributes of over one million products in the computer and consumer electronics categories
 * and to read user product reviews in order to make informed purchase decisions. Consumers can then
 * research the latest promotional and pricing information on products listed at a wide selection of
 * online merchants, and read user reviews on those merchants.
 * The copyrights is reserved by http://www.mezimedia.com.
 * Copyright (c) 2005, Mezimedia. All rights reserved.
 *
 * @author     james.huang <james_huang@mezimedia.com>
 * @copyright  (C) 2004-2008 Mezimedia.com
 * @license    http://www.mezimedia.com  PHP License 5.2
 * @version    CVS: $Id: UserController.php
 * @link       http://pms.mezimedia.com/
 * @deprecated File deprecated in Release 1.0
 */

class TaskConfigure extends Zend_Db_Table {
	public $_db = null;
	public $_entities = array ();

	public function __construct() {
		$this->_db = Zend_Registry :: get('db_adapter');
		$this->_name = 'tt_task_configure';
		$this->_primary = 'name';
		parent::_setup();
		self::_init();
	}

	public function _init() {
		$cache = Zend_Registry :: get('cache');
		// get from cache
		if(!$this->_entities = $cache->load('configure')) {
			// initialize configure
			$configure = $this->getAllConfigure();
			foreach ($configure as $config) {
				$value = explode(',', $config['value']);
				if ($config['name'] != 'authority')
					array_unshift($value, '--');
				$this->_entities[$config['name']] = $value;
			}
			//echo "<pre>";print_r($this->_entities);echo "</pre>";exit;
			// Build resource multi-userlist
			$this->_entities['manager']		= array('--');
			$this->_entities['engineer']	= array('--');
			$this->_entities['resource']	= array('--');
			$this->_entities['requester']	= array('--');
			$this->_entities['qa']			= array('--');
			$user_list = $this->getUserList();
			//echo "<pre>";print_r($user_list);echo "</pre>";
			foreach ($user_list as $user) {
				$abs = $user['roleAbs'];
				switch ($abs) {
					case 'RT' :
						$this->_entities['requester'][$user['userId']] = $user['userName'];
						break;
					case 'PM' :
						$this->_entities['manager'][$user['userId']] = $user['userName'];
						break;
					case 'ST' :
						$this->_entities['engineer'][$user['userId']] = $user['userName'];
						break;
					case 'QA' :
						$this->_entities['qa'][$user['userId']] = $user['userName'];
						break;
				}
				$this->_entities['resource'][$abs] = $user;
			}
			// Push TicketType into the configure
			$this->_entities['ticket_type'] = array('common' => 'Common Ticket', 'roadmap' => 'Roadmap');
			$cache->save($this->_entities, 'configure');
			//echo "<pre>";print_r($this->_entities);echo "</pre>";
		}
//		Utils::dumper($this->_entities);
		return $this->_entities;
	}

	public function getAllConfigure() {
		$sql = 'select * from ' . $this->_name;
		return $this->_db->fetchAll($sql);
	}

	public function getUserList() {
		$sort_user_list = array ();
		$sql  = 'select u.id as userId, u.name as userName, r.abs as roleAbs ';
		$sql .= 'from tt_user u, tt_role r, tt_rsh_user_role ur ';
		$sql .= 'where u.id = ur.user_id and ur.role_id = r.id ';
		$sql .= 'order by r.id, u.name';
		$res = $this->_db->query($sql);
		if ($res) {
			$sort_user_list = $res->fetchAll();
		}
//		Utils::dumper($sort_user_list);
		return $sort_user_list;
	}

	public function getConfigCols() {
		$columns = array ();
		$sql = 'select distinct name from tt_task_configure';
		$dat = $this->_db->fetchAll($sql);
		if (sizeof($dat) > 0) {
			foreach ($dat as $row) {
				array_push($columns, $row['name']);
			}
		}
		return $columns;
	}

	public function updateConfig($data) {
		foreach ($data as $k => $v) {
			$sql = 'update tt_task_configure set value=\''.$v.'\' where name=\''.$k.'\'';
			$this->_db->query($sql);
		}
		return true;
	}
}
?>